﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Read.Application.Command.TeamCommand;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Read.Application.CommandHander.TeamCommandHander
{
    public class QueryTeamCommandHander : IRequestHandler<QueryTeamCommand, APIPaging<List<Team>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public QueryTeamCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIPaging<List<Team>>> Handle(QueryTeamCommand request, CancellationToken cancellationToken)
        {
            APIPaging<List<Team>> result = new APIPaging<List<Team>>();

            var item = await db.Queryable<Team>().Where(x => x.Del == false).ToListAsync();

            result.TotalCount = item.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            item = item.OrderByDescending(x => x.TeamId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            result.code = APIResultCode.success;
            result.msg = "查询交接班信息成功";
            result.data = item;

            return result;
        }
    }
}
